<?php

namespace Question\Controller;

/**
 * 题目
 */
class Info extends \App\Application {

	private $_questionService;

	/**
	 * 构造函数
	 */
	public function __construct($appName, $controllerName, $actionName) {
		parent::__construct($appName, $controllerName, $actionName);
		$this->_questionService = \Lsf\Loader::service('Question', FALSE, APP_NAME_QUESTION);
	}

	/**
	 * 题目列表
	 */
	public function list() {
		$params = $this->post('', TRUE);
		if (!isset($params['question_set_id']) || empty($params['question_set_id'])) {
			$questionSetId = $this->get('question_set_id', TRUE);
			if (empty($questionSetId)) {
				return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_set_id');
			}else{
				$params['question_set_id'] = $questionSetId;
			}
		}
//		if (!isset($params['last_record_id']) || empty($params['last_record_id'])) {
//			$params['last_record_id'] = 0;
//		}
		$pageSize = $this->get('page_size', TRUE);
		$pageNo = $this->get('page_no', TRUE);
		// 获取数据
		if($pageSize > 0 && $pageNo > 0){
			// web
			$result = $this->_questionService->getQuestionListPage($params['question_set_id'], $pageSize, $pageNo,0);
		}else {
			// vx
			$result = $this->_questionService->getQuestionListWX(1);
			//$result = $this->_questionService->getQuestionListPage($params['question_set_id'], $params['page_size'], $params['page_no'],1);
		}
		if(!is_array($result) || empty($result)){
			return $this->json(4,'获取题目失败', []);
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 删除题目
	 */
	public function del() {
		$params = $this->post('', TRUE);
		if (!isset($params['question_id']) || empty($params['question_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_id');
		}
		// 修改数据
		$result = $this->_questionService->delQuestion($params['question_id']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'删除题目失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 题目修改
	 */
	public function edit() {
		$params = $this->post('', TRUE);
		if (!isset($params['question_id']) || empty($params['question_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_id');
		}
		if (!isset($params['question_desc']) || empty($params['question_desc'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_desc');
		}
		if (!isset($params['answer']) || empty($params['answer'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'answer');
		}
		if (!isset($params['choice_info']) || empty($params['choice_info'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'choice_info');
		}
		// 修改数据
		$result = $this->_questionService->editQuestion($params['question_id'],$params['question_desc'],$params['answer'],$params['choice_info']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'修改题目失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 增加题目
	 */
	public function add() {
		$params = $this->post('', TRUE);
		if (!isset($params['question_set_id']) || empty($params['question_set_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_set_id');
		}
		if (!isset($params['question_desc']) || empty($params['question_desc'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_desc');
		}
		if (!isset($params['answer']) || empty($params['answer'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'answer');
		}
		if (!isset($params['choice_info']) || empty($params['choice_info'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'choice_info');
		}
		// 参数检查
		if(!is_array($params['choice_info'])){
			return $this->errParamValueInvalid(ECODE_PARAM_VALUE_INVALID, 'choice_info', $params['choice_info']);
		}
		foreach ($params['choice_info'] as $choice){
			if(!is_array($choice)){
				return $this->errParamValueInvalid(ECODE_PARAM_VALUE_INVALID, 'choice_info', $params['choice_info']);
			}
			if(!isset($choice['title']) || !isset($choice['content']) || empty($choice['title']) || empty($choice['content'])){
				return $this->errParamValueInvalid(ECODE_PARAM_VALUE_INVALID, 'choice_info', $params['choice_info']);
			}
		}
		// 插入数据
		$result = $this->_questionService->addQuestion($params['question_set_id'],$params['question_desc'],$params['answer'],$params['choice_info']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'新增题目失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 轮询题目
	 */
	public function reload(){
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		// 插入数据
		$result = $this->_questionService->reloading($params['device_id']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'轮询题目失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 主持人操作
	 */
	public function operate(){
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		if (!isset($params['operate_type']) || empty($params['operate_type'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'operate_type');
		}
		// 插入数据
		$result = $this->_questionService->operate($params['device_id'],$params['operate_type'],$params['question_id']);
		if(!is_array($result)){
			return $this->json(4,[],'主持人操作失败');
		}
		if(!isset($result['question_info'])){
			return $this->json(ECODE_SUCCESS, $result);
		}
		if(empty($result['question_info'])){
			return $this->json(5,[], '没有更多题目');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}



}
